package dp.mobile.level;

import java.util.ArrayList;

import org.w3c.dom.Node;

import android.util.Log;

import dp.mobile.entity.Enemy;
import dp.mobile.game.Sprite;
import dp.mobile.glwrapper.GLTexture;
import dp.mobile.glwrapper.GLTiledTexture;
import dp.mobile.glwrapper.GameRenderer;
import dp.mobile.system.Utilities;

public class Wave {
	private static final String TAG = "Wave";
	public Wave() {
		//initialize
		mEnemyMax		= 0;
		mEnemyCount		= 0;
		mSpawnTimer		= 0;
		mSpawnDelay		= 0;
		mEnemyData		= null;
		mEnemyImages	= null;
	}
	
	public Wave(Node root) {
		this();
		
		int childCount = root.getChildNodes().getLength();
		
		for (int i = 0; i < childCount; ++i) {
			Node child = root.getChildNodes().item(i);
			if (child.getNodeType() == Node.TEXT_NODE) continue;
			
			String tag 		= child.getNodeName();
			String value	= child.getFirstChild().getNodeValue();
			
			if (tag.equals("bonus"))		mBonus 		= Integer.parseInt(value);
			else if (tag.equals("count"))	mEnemyMax 	= Integer.parseInt(value);
			else if (tag.equals("delay"))	mSpawnDelay = Integer.parseInt(value);
			else if (tag.equals("data"))	mEnemyData 	= child;
			else if (tag.equals("image"))	mEnemyFile	= value;
		}
	}
	
	public Node getEnemyData() {
		return mEnemyData;
	}
	
	public void loadEnemyImage() {
		//Set data
		int Row		= 1;
		int Column	= 4;

		//Create image array
		mEnemyImages	= new GLTiledTexture[4][Row * Column];
		
		//For each direction
		for (int j = 0; j < 4; j++) {
			//Get image data
			GLTexture mainText	= GameRenderer.instance().allocateTexture(Utilities.ENEMY_FOLDER + "/" + mEnemyFile + "_" + j + ".png");
			//For each frame
			for (int l = 0; l < mEnemyImages[j].length; l++) {
				final float left 	= (float)l 		/ (float)mEnemyImages[j].length;
				final float right 	= (float)(l + 1)/ (float)mEnemyImages[j].length;
				
				float[] crop = new float[] {
					left,	1,
					right,	1,
					left,	0,
					right,	0
				};
				
				mEnemyImages[j][l] = new GLTiledTexture(mainText, crop);
			}
		}
	}
	
	public void unloadEnemyImage() {
		//Destroy image
		/// TODO : unload enemies
	}
	
	public int getBonus()						{	return mBonus;						}
	public GLTiledTexture[][] getEnemyImages()	{	return mEnemyImages;				}
    public boolean isWaveFinished()				{   return (mEnemyCount >= mEnemyMax);	}
    
    public void setEnemyCount(int count) {
    	mEnemyCount = count;
    }
    
    public void setMaxEnemy(int max) {
    	mEnemyMax = max;
    }
    
    public void setSpawnTimer(long timer) {
    	mSpawnTimer = timer;
    }
    
    public void setSpawnDelay(long delay) {
    	mSpawnDelay = delay;
    }

	public Node[] spawnEnemy(long time) {
		//Default value
		Node[] Spawn = null;
		ArrayList<Node> Spawns	= new ArrayList<Node>();

		//If not max
		if (mEnemyCount < mEnemyMax) {
			//Increase timer
			mSpawnTimer += time;

			//If delayed enough
			if (mSpawnTimer >= mSpawnDelay) {
				//While more
				while (mSpawnTimer > mSpawnDelay && mEnemyCount < mEnemyMax) {
					//Add
					Spawns.add(mEnemyData);
					mEnemyCount++;

					//Reduce timer
					mSpawnTimer -= mSpawnDelay;
				}

				//Transfer to array
				Spawn = new Node[Spawns.size()];
				for (int i = 0; i < Spawns.size(); i++) Spawn[i] = Spawns.get(i);
			}
		}

		//Return spawned
		return Spawn;
	}
	
	//Member
	protected int		mBonus;
	protected int		mEnemyMax;
	protected int		mEnemyCount;
	protected long		mSpawnTimer;
	protected long		mSpawnDelay;
	protected Node		mEnemyData;
	protected String	mEnemyFile;
	protected GLTiledTexture[][]	mEnemyImages;
}
